package com.example.children.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.children.DTO.TeacherClassDTO;
import com.example.children.FileService.teacherInterface;
import com.example.children.Mapper.TeacherMapper;
import com.example.children.Model.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * StudentService 服务层
 */
@Service
public class TeacherService extends ServiceImpl<TeacherMapper, Teacher> implements teacherInterface{

    @Autowired
    private TeacherMapper teacherMapper;

    // 插入一个新
    public void insertTeacher(Teacher teacher) {
        teacherMapper.insert(teacher);
    }


    // 查询所有
    public List<Teacher> getAllTeachers() {
        return teacherMapper.selectList(null);  // 查询所有数据
    }


    // 根据 id 更新 信息
    public void updateTeacher(Teacher teacher) {
        teacherMapper.updateById(teacher);
    }

    // 根据 id 删除
    public void deleteTeacher(String id) {
        teacherMapper.deleteById(id);
    }


    // 方法来获取分页数据
    public Page<TeacherClassDTO> getTeacherAllWithClassPage(int pageNo, int pageSize) {
        // 计算分页的偏移量
        int offset = (pageNo - 1) * pageSize;

        // 获取当前页的数据
        List<TeacherClassDTO> records = teacherMapper.getTeacherAllWithClassPage(offset, pageSize);
        // 获取总记录数
        int total = teacherMapper.countTotalRecords();
        // 构造并返回分页对象
        Page<TeacherClassDTO> page = new Page<>();
        page.setTotal(total);
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        page.setRecords(records);

        return page;
    }


    public List<TeacherClassDTO> selectToTeachers(TeacherClassDTO teacher) {
        return teacherMapper.selectToTeachers(teacher);

    }


}
